package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.Contract;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.customer.SuppliersAssessService;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ContractService;
import com.xpsoft.oa.service.project.ProjectService;

public class ContractFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(ContractFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		//内部合同数据
		String proId = (String)pi.getVariable("contract.project.id");
		String category = (String)pi.getVariable("contract.category");
		String content = (String)pi.getVariable("contract.content");
		String contractNo = (String)pi.getVariable("contract.contractNo");
		String suppliersId = (String)pi.getVariable("contract.suppliersAssess.suppliersId");
		String property = (String)pi.getVariable("contract.property");
		String suppliersLevel = (String)pi.getVariable("contract.suppliersLevel");
		String contractVersion = (String)pi.getVariable("contract.contractVersion");
		String contractForm = (String)pi.getVariable("contract.contractForm");
		BigDecimal contractAmount = (BigDecimal)pi.getVariable("contract.contractAmount");
		String payType = (String)pi.getVariable("contract.payType");
		String appointment = (String)pi.getVariable("contract.appointment");
		Integer originalCopyNum = (Integer)pi.getVariable("contract.originalCopyNum");
		Integer giveCopyNum = (Integer)pi.getVariable("contract.giveCopyNum");
		String attachIds = (String)pi.getVariable("contract.attachIds");
		String attachFiles = (String)pi.getVariable("contract.attachFiles");
		
		ContractService contractService = (ContractService)AppUtil.getBean("contractService");
		Contract contract = new Contract();
		contract.setCategory(category);
		contract.setContent(content);
		contract.setProperty(property);
		contract.setContractNo(contractNo);
		contract.setSuppliersLevel(suppliersLevel);
		contract.setContractVersion(contractVersion);
		contract.setContractForm(contractForm);
		contract.setContractAmount(contractAmount);
		contract.setPayType(payType);
		contract.setAppointment(appointment);
		contract.setOriginalCopyNum(originalCopyNum);
		contract.setGiveCopyNum(giveCopyNum);
		contract.setAttachFiles(attachFiles);
		contract.setAttachIds(attachIds);
		contract.setType(Contract.INNER_TYPE);//内部合同类型
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			contract.setProject(projectService.get(Long.valueOf(proId)));
			contract.setContractNo(contractService.getNewContractNoByProId(Long.valueOf(proId)));
		}
		if(StringUtils.isNotBlank(suppliersId)){
			SuppliersAssessService suppliersAssessService = (SuppliersAssessService)AppUtil.getBean("suppliersAssessService");
			contract.setSuppliersAssess(suppliersAssessService.get(Long.valueOf(suppliersId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		contract.setProcessRunId(processRun.getRunId());
		
		contractService.save(contract);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
